<template>
  <div id="app-container">
    <el-form>
      <el-form-item
        label="考试科目管理"
        style="margin-bottom: 10px"
      ></el-form-item>
    </el-form>
    <!--数据列表-->
    <el-table
      :header-cell-style="{ background: '#ececf1', color: '#636870' }"
      :data="subjectList"
      style="width: 100%"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="title" label="考试名称" />
      <el-table-column prop="subject" label="考试科目管理" />
      <el-table-column prop="create_time" label="创建时间" />
      <el-table-column v-if="roles !== '学生'" label="操作" width="180">
        <template v-slot="{ row }">
          <div v-if="isShowEdit(row)">
            <el-button
              type="text"
              style="color: #586cb1"
              @click="handleEdit(row)"
              >编辑</el-button
            >
          </div>
          <div v-else>
            <el-button
              :disabled="row.children.length === 9"
              type="text"
              style="color: #586cb1"
              @click="showDialog(row)"
              >添加</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 编辑考试学科  -->
    <el-dialog
      title="编辑考试科目"
      :visible.sync="dialogVisible"
      :show-close="false"
      width="30%"
      :before-close="handleClose"
    >
      <el-form
        ref="form"
        :model="subjectInfo"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="科目名称" prop="subject">
          <el-input v-model="subjectInfo.subject" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button
          style="color: #fff; background-color: #586cb1; border-color: #586cb1"
          type="primary"
          @click="handleSave"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 新增考试学科 -->
    <el-dialog
      title="新增考试学科"
      :visible.sync="dialogSubject"
      width="30%"
      :show-close="false"
      :before-close="handleCloseSubject"
    >
      <el-tag
        :key="tag.id"
        v-for="tag in dynamicTags"
        closable
        :disable-transitions="true"
        @close="handleCloseTag(tag)"
      >
        {{ tag }}
      </el-tag>
      <el-input
        class="input-new-tag"
        v-if="inputVisible"
        v-model="inputValue"
        ref="saveTagInput"
        size="small"
        @keyup.enter.native="handleInputConfirm"
        @blur="handleInputConfirm"
      >
      </el-input>
      <el-button v-else class="button-new-tag" size="small" @click="showInput"
        >+ 考试学科</el-button
      >
      <span slot="footer" class="dialog-footer">
        <el-button
          style="color: #fff; background-color: #586cb1; border-color: #586cb1"
          size="mini"
          @click="addSubjectBtn"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as courseApi from "@/api/course";
import { mapGetters } from "vuex";

export default {
  name: "Major",
  data() {
    return {
      // 学科列表
      subjectList: [],
      // 学科信息
      subjectInfo: {
        subject: "",
      },
      // 考试id
      testId: "",
      // 是否显示弹窗
      dialogVisible: false,
      // 表单验证规则
      rules: {
        subject: [
          { required: true, message: "请输入考试科目及名称", trigger: "blur" },
        ],
      },
      // 新增考试学科是否可见
      dialogSubject: false,
      //tag标签增加学科
      dynamicTags: [],
      inputVisible: false,
      inputValue: "",
    };
  },
  computed: {
    // 是否显示操作列
    isShowEdit() {
      return (row) => {
        return row.subject;
      };
    },
    ...mapGetters(["roles"]),
  },
  mounted() {
    this.getsubjectList();
  },
  methods: {
    //tag标签增加学科
    handleCloseTag(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.dynamicTags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = "";
    },
    //添加考试科目打开
    showDialog(row) {
      this.dialogSubject = true;
      this.testId = row.id;
    },
    //确认增加考试科目
    addSubjectBtn() {
      if (this.dynamicTags.length !== 9) {
        this.$notify({
          title: "警告",
          message: "请填写9门考试科目",
          type: "warning",
        });
      } else {
        let newArr = [this.dynamicTags, this.testId];
        courseApi
          .addSubject(newArr)
          .then((res) => {
            this.$message.success(res.msg);
          })
          .catch();
        //关闭模态框
        this.dialogSubject = false;
        //重新获取数据
        this.getsubjectList();
      }
    },
    // 获取考试和学科列表
    getsubjectList() {
      courseApi.getSubject().then((res) => {
        this.subjectList = res.data;
      });
    },
    // 编辑
    handleEdit(row) {
      // 修改id
      const id = row.id.split("-")[1];
      this.subjectInfo = JSON.parse(JSON.stringify(row));
      this.subjectInfo.id = id;
      this.dialogVisible = true;
    },
    // 确认编辑
    handleSave() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 编辑
          courseApi.updateSubject(this.subjectInfo).then((res) => {
            this.$message({
              type: "success",
              message: "编辑成功!",
            });
            this.dialogVisible = false;
            this.getsubjectList();
          });
        }
        this.subjectInfo = {
          subject: "",
        };
      });
    },
    //关闭编辑学科对话框之前
    handleClose() {
      this.$refs["form"].clearValidate();
      this.dialogVisible = false;
    },
    // 关闭新增学科对话框
    handleCloseSubject(done) {
      // 关闭对话框前，重置表单数据
      this.dynamicTags = [];
      this.testId = "";
      done();
    },
    // 关闭弹窗
    // handleClose() {
    //   this.dialogVisible = false;
    //   this.subjectInfo = {
    //     subject: "",
    //   };
    // },
  },
};
</script>

<style scoped>
/*
  el-table样式穿透 表格居中
*/
::v-deep .el-table th > .cell {
  text-align: center;
}

::v-deep .el-table .cell {
  text-align: center;
}
#app-container {
  padding: 20px;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
